<?php
/**
 * Created by PhpStorm.
 * User: bob-chou
 * Date: 17/2/12
 * Time: 20:01
 */
namespace Common\Model;

use Think\Model;

/**
 * 文章内容
 * Class NewsModel
 * @package Common\Model
 */
class NewsModel extends Model {
    /**
     * @var \Model|string|Model
     */
    private $_db = '';

    /**
     * NewsModel constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->_db = M('news');
    }

    /**
     * @param $data
     * @return int
     */
    public function insert($data)
    {
        if (!$data || !is_array($data)){
            return 0 ;
        }
        $data['create_time'] = time();
        $data['username'] = getAdminUser();
        $res = $this->_db
            ->add($data);
        return $res;
    }

    /**
     * 文章列表以及分页
     * @param $data
     * @param $page
     * @param int $pageSize
     * @return mixed
     */
    public function getNews($data , $page , $pageSize = 10)
    {
        $conditions = $data;
        if (isset($data['title']) && $data['title']){
            $conditions['title'] = ['like' , '%'.$data['title'].'%'];
//            $conditions['title'] = rtrim($data['title']);
        }
        if (isset($data['carid']) && $data['carid']){
            $conditions['catid'] = intval($data['carid']);
        }
        $offset = ($page - 1) * $pageSize;
        $res = $this->_db
            ->where($conditions)
            ->order('listorder desc , news_id desc')
            ->limit($offset , $pageSize)
            ->select();
        return $res;
    }

    /**
     * 文章总数
     * @param array $data
     * @return mixed
     */
    public function getNewsCount($data = [])
    {
        $conditions = $data;
        if (isset($data['title']) && $data['title']){
            $conditions['title'] = $data['title'];
        }
        if (isset($data['carid']) && $data['carid']){
            $conditions['catid'] = intval($data['carid'] );
        }
        $res = $this->_db
            ->where($conditions)
            ->count();
        return $res;
    }

    /**
     * 根据id修改文章内容
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id , $data)
    {
        if (!$id || !is_numeric($id)){
            throw_exception("idzzzzzzz不合法");
        }
        if (!$data || !is_array($data)){
            throw_exception("更新的数据不合法");
        }
        $res = $this->_db
            ->where( "news_id = " . $id)
            ->save($data);
        return $res;
    }

    /**
     * 删除文章【将状态改成-1】
     * @param $id
     * @param $status
     * @return bool
     */
    public function updateStatusById($id , $status)
    {
        if (!$id || !is_numeric($id)){
            throw_exception("id 不存在");
        }
        if (!is_numeric($status)){
            throw_exception("status不能为非数组");
        }
        $data['status'] = $status;
        $res = $this->_db
            ->where("news_id = " . $id)
            ->save($data);
        return $res;
    }

    /**
     * 文章排序
     * @param $id
     * @param $listorder
     * @return bool
     */
    public function updateListorderById($id , $listorder)
    {
        if (!$id){
            throw_exception('ID不合法');
        }
        $data['listorder'] = intval($listorder);
        $res = $this->_db
            ->where("news_id = " . $id)
            ->save($data);
        return $res;
    }

    /**
     * 文章推荐位
     * @param array $newsId
     * @return mixed|void
     */
    public function getNewsByIdIn(array $newsId) {
        if (!is_array($newsId)){
            return show(0 , "参数不合法");
        }
        $data = [
          'news_id' => ['in' , implode(',' , $newsId)],
        ];
        $res = $this->_db
                ->where($data)
                ->select();
        return $res;
    }
}